var express = require('express');
var router = express.Router();
var EventProxy = require('eventproxy');
var util = require('util');

var logger = require("../models/logger")
var News = require("../models/News");
var resultJson = require("../result-json");
var Utils = require("../utils");
var Consts = require("../consts");

router.post('/', function(req, res) {
    res.send('respond with a resource');
});

router.post("/detail", function(req, res) {
    var news = req.body;
    var _id=news._id;
    News.findById(news._id, function(err, news) {
        if (err ) {
            logger.error(err);
            return res.send(resultJson(2));
        }
        if(news.view==null){
            News.update({
                _id: _id
            }, {
                $set: {
                    view: 1
                }
            }, function(err) {
                if (err)
                    return res.send(resultJson(2));
            }); 
            res.send(resultJson(0, news));
        }else{
           News.update({
                _id: _id
            }, {
                $set: {
                    view:++news.view
                }
            }, function(err) {
                if (err)
                    return res.send(resultJson(2));
            });
            res.send(resultJson(0, news));
        }
        res.send(resultJson(0, news));
    });
});

router.post('/fc', function(req, res) {
    list(req, res);
});
router.post('/cl', function(req, res) {
    list(req, res);
});
router.post('/tr', function(req, res) {
    list(req, res);
});
router.post('/sp', function(req, res) {
    list(req, res);
});
router.post('/bu', function(req, res) {
    list(req, res);
});
router.post('/wo', function(req, res) {
    list(req, res);
});
router.post('/li', function(req, res) {
    list(req, res);
});


function list(req, res) {
    var newc = req.body;
    var _id=newc.chanelId;
    var option = {};
    if(newc.chanelId)
      option.chanelId = newc.chanelId;
    // var start = newc.start || 0;
    // var count = newc.count || Consts.MAX;

    News.find(option).skip(0).limit(10).sort({
        create: -1
    }).exec(function(err, news) {
         if (err) {
            logger.error(err);
            return res.send(resultJson(2));
        }
       res.send(resultJson(0, news)); 
});
};


router.post('/fenye', function(req, res) {
    var event = req.body;
    //var num = req.body.num;
    //console.log(num);
    var option = {};
    //var nnum=num*10;
    var start = event.start || 0;
    var count = event.count || Consts.MAX;
    console.log("start:"+start);
    console.log("count:"+count);
    News.find(option).skip(start).limit(count).sort({
        create: -1
    }).exec(function(err, events) {
        if (err) {
            logger.error(err);
            return res.send(resultJson(2));
      }
      res.send(resultJson(0, events));
    })
});

// function list(req, res) {
// //router.post('/list', function(req, res) {
    // var option = {};
    // News.find(option).sort({
        // date: -1
    // }).exec(function(err, news) {
        // if (err ) {
            // logger.error(err);
            // return res.send(resultJson(2));
        // }
// 
        // var length = news && news.length || 0;
        // var array = [];
        // for(var i=0; i<length; i++) {
            // var find = false;
            // for(var j=0; j<array.length; j++) {
                // if(news[i].chanelId == array[j].chanelId) {
                    // array[j].news.push(news[i]);
                    // console.log("find one, chanelId:",news[i].chanelId)
                    // find = true;
                    // break;
                // }
            // }
            // if(find == false) {
                // console.log("not find, new one. chanelId:",news[i].chanelId)
                // var item = {};
                // item.chanelId = news[i].chanelId;
                // item.news = [];
                // item.news.push(news[i]);
                // array.push(item);
            // } 
        // }
        // res.send(resultJson(0, array));
    // });  
// //});
// };


router.post('/list', function(req, res) {
    var option = {};
    News.find(option).sort({
        date: -1
    }).exec(function(err, news) {
        if (err ) {
            logger.error(err);
            return res.send(resultJson(2));
        }

        var length = news && news.length || 0;
        var array = [];
        for(var i=0; i<length; i++) {
            var find = false;
            for(var j=0; j<array.length; j++) {
                if(news[i].chanelId == array[j].chanelId) {
                    array[j].news.push(news[i]);
                    console.log("find one, chanelId:",news[i].chanelId)
                    find = true;
                    break;
                }
            }
            if(find == false) {
                console.log("not find, new one. chanelId:",news[i].chanelId)
                var item = {};
                item.chanelId = news[i].chanelId;
                item.news = [];
                item.news.push(news[i]);
                array.push(item);
            } 
        }
        res.send(resultJson(0, array));
    });  
});


module.exports = router;